<?php
class Currency_Controller extends AdminController {
    
    public function __construct() {
        parent::__construct('currency');
        $this->setDefaultListOrder('code');
    }
    
    function all() {
        $this->initPagination('currency');
        
        $this->viewData['currencyList'] = $this->dao->queryAll(
                $this->orderLimitAndOffset('SELECT * FROM currency'));
        
        $this->renderSubview('admin/currency/currencyList');
    }
    
    function display() {
        $code = $this->input->get('code');

        if (! valid::isCode($code)) {
            $this->redirectToList('error.invalidId');
        }
        
        $currency = $this->dao->getByCode('currency', $code);
        if (! $currency) {
            $this->redirectToList('error.recordNotFound');
        }
        $this->viewData['currency'] = $currency;
        
        $this->renderSubview('admin/currency/currencyDisplay');
    }
    
    function edit() {
        $code = $this->input->get('code');
        $currency = array();
        
        if ($code) {
            $currency = $this->dao->getByCode('currency', $code);
            if (! $currency) {
                $this->redirectToList('error.recordNotFound');
            }
        }
        else {
            // Set default insert values here
        }
        
        $this->viewData['currency'] = $currency;
        $this->renderSubview('admin/currency/currencyEdit');
    }
    
    function save() {
        $currency = $this->postToDbValues();
        $code = $currency['code'];
        $this->viewData['currency'] = $currency;
        
        // Basic validation
        $validator = new EntityValidator($currency, 'currency', $this->msg);
        $validator->required('code');
        $validator->required('name');
        $validator->required('name_native');
        if ($this->msg->hasErrors()) {
            $this->renderSubview('admin/currency/currencyEdit');
            return;
        }

        // Database validation
        if ($this->dao->alreadyExists('currency', 'name', $currency, 'code')) {
            $this->msg->error('currency.error-alreadyExists-name', $currency['name']);
        }
        if (! sql::isPersistent($currency)) {
            $existingCurrency = $this->dao->getByCode('currency', $code);
            if ($existingCurrency) {
                $this->msg->error('currency.error-alreadyExists-code', $currency['code']);
            }
        }
        if ($this->msg->hasErrors()) {
            $this->renderSubview('admin/currency/currencyEdit');
            return;
        }

        // Insert or update
        if (sql::isPersistent($currency)) {
            $this->dao->update('currency', $currency, 'code');
            $this->msg->info('currency.editSuccess', $currency['name']);
        }
        else {
            $id = $this->dao->insert('currency', $currency);
            $this->msg->info('currency.createSuccess', $currency['name']);
        }

        $this->redirectToList();
    }
    
    function delete() {
        $code = $this->input->get('code');

        if (! valid::isCode($code)) {
            $this->msg->error('error.invalidId');
            $this->redirectToList('error.invalidId');
        }

        // Basic validation
        $currency = $this->dao->getByCode('currency', $code);
        if (! $currency) {
            $this->redirectToList('error.recordNotFound');
        }
        
        $this->dao->deleteByCode('currency', $code);
        
        $this->msg->error('currency.deleteSuccess', $currency['name']);
        $this->redirectToList();
    }
}
?>
